let userInfo = {}
let db = wx.cloud.database()
let commentScribeId = 'jhu_bPqHDvOAXXe5duuy8qQSnvNccJpJ703M2pyQSk0'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    blogid: String,
    commentlength: Number,
    blog: Object,
  },
  externalClasses: ['iconfont', 'icon-pinglun', 'icon-fenxiang', 'icon-delete'],
  /**
   * 组件的初始数据
   */
  data: {
    // 授权信息是否显示
    isLoginShow: false,
    // 评论框是否显示
    isModalShow: false,
    content: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onComment() {
      // 判断用户是否授权
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: (res) => {
                userInfo = res.userInfo
                // 显示评论框
                this.setData({
                  isModalShow: true
                })
              }
            })
          } else {
            this.setData({
              isLoginShow: true
            })
          }
        }
      })
    },
    onLoginsuccess(res) {
      userInfo = res.detail
      // 登录框隐藏 评论框显示
      this.setData({
        isLoginShow: false
      }, () => {
        this.setData({
          isModalShow: true
        })
      })
    },
    onLoginfail() {
      wx.showModal({
        title: '提示',
        content: '授权用户才能进行评论',
        showCancel: false,
        confirmColor: '#cc0000'
      })
    },
    getComment(e) {
      this.setData({
        content: e.detail.value
      })
    },
    sendComment(e) {
      let comment = e.detail.value.content
      if (comment.trim() == "") {
        wx.showModal({
          title: '提示',
          content: '评论不能为空',
          showCancel: false,
          confirmColor: '#cc0000'
        })
        return
      }
      //推送订阅消息
      wx.requestSubscribeMessage({
        tmplIds: [commentScribeId],
        success: (res) => {
          if (res[commentScribeId] == 'accept') {
            wx.showLoading({
              title: '加载中',
              mask: true,
            })
            db.collection('blog-comment').add({
              data: {
                content: comment,
                creatTime: db.serverDate(),
                blogId: this.properties.blogid,
                avatarUrl: userInfo.avatarUrl,
                nickName: userInfo.nickName
              }
            }).then((res) => {
              wx.cloud.callFunction({
                name: 'sendMessage',
                data: {
                  content: comment,
                  templateId: commentScribeId,
                  blogId: this.properties.blogid
                },
              }).then((res) => {
                console.log(res)
              })
              wx.hideLoading()
              wx.showToast({
                title: '评论成功',
              })
              this.setData({
                isModalShow: false,
                content: '',
              })
              this.triggerEvent('commentSuccess')
            })
          } else {
            wx.showModal({
              title: '提示',
              content: '授权才能收到评论消息提示',
              showCancel: false,
              confirmColor: '#cc0000'
            })
          }
        }
      })

    },
  }
})